package dao

import javax.inject.Inject
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.{ExecutionContext, Future}

class GenomeDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
                         (implicit exec: ExecutionContext) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insertGenome(row: Seq[GenomeRow]): Future[Unit] = {
    db.run(Genome ++= row).map(_ => ())
  }

  def delete2:Future[Unit] = {
    db.run(Genome.filter(_.species === "2").delete).map(_=>())
  }

  def updateSpecie : Future[Unit] = {
    db.run(Genome.filter(_.species === "1").map(_.species).update("CON")).map(_=>())
  }

  def getAllInfo: Future[Seq[GenomeRow]] = {
    db.run(Genome.result)
  }

  def getById(id:Int) : Future[GenomeRow] = {
    db.run(Genome.filter(_.id === id).result.head)
  }

  def getByIds(id: Seq[Int]): Future[Seq[GenomeRow]] = {
    db.run(Genome.filter(_.id.inSetBind(id)).result)
  }


  def getByGeneId(geneid:String) : Future[GenomeRow] = {
    db.run(Genome.filter(_.geneid === geneid).result.head)
  }

  def getByGeneIds(geneids: Seq[String], species: String): Future[Seq[GenomeRow]] = {
    db.run(Genome.filter(_.species === species).filter(_.geneid.inSetBind(geneids)).result)
  }

  def getByRange(species: String, chr: String, start: Int, end: Int): Future[Seq[GenomeRow]] = {
    db.run(Genome.filter(_.species === species).filter(_.chr === chr).filter(_.start >= start).filter(_.end <= end).result)
  }


}
